package com.weijian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weijian.dal.entity.MatchDetail;
import com.weijian.dal.entity.Route;
import com.weijian.dal.entity.Shipment;
import com.weijian.dto.match.MatchDetailWithRouteAndShipmentDTO;
import com.weijian.mapper.MatchDetailMapper;
import com.weijian.service.MatchDetailService;
import com.weijian.service.RouteService;
import com.weijian.service.ShipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 匹配详情服务实现类
 */
@Service
public class MatchDetailServiceImpl extends ServiceImpl<MatchDetailMapper, MatchDetail> implements MatchDetailService {

    @Autowired
    private RouteService routeService;

    @Autowired
    private ShipmentService shipmentService;

    @Override
    public List<MatchDetail> getMatchDetailsByDataId(Integer dataId) {
        return this.lambdaQuery()
                .eq(MatchDetail::getDataId, dataId)
                .list();
    }

    @Override
    public List<MatchDetailWithRouteAndShipmentDTO> getMatchDetailsWithRouteAndShipmentByDataId(Integer dataId) {
        // 查询匹配详情
        List<MatchDetail> matchDetails = this.lambdaQuery()
                .eq(MatchDetail::getDataId, dataId)
                .list();

        // 收集所有路线ID和货物ID
        List<Integer> routeIds = matchDetails.stream()
                .map(MatchDetail::getRouteId)
                .filter(id -> id != null && id != -1) // 过滤掉Self分配的情况
                .distinct()
                .collect(Collectors.toList());

        List<Integer> shipmentIds = matchDetails.stream()
                .map(MatchDetail::getShipmentId)
                .filter(id -> id != null)
                .distinct()
                .collect(Collectors.toList());

        // 批量查询路线和货物信息
        List<Route> routes = routeService.listByIds(routeIds);
        List<Shipment> shipments = shipmentService.listByIds(shipmentIds);

        // 转换为Map方便查找
        Map<Integer, Route> routeMap = routes.stream()
                .collect(Collectors.toMap(Route::getId, route -> route));

        Map<Integer, Shipment> shipmentMap = shipments.stream()
                .collect(Collectors.toMap(Shipment::getId, shipment -> shipment));

        // 组装结果
        return matchDetails.stream()
                .map(matchDetail -> {
                    MatchDetailWithRouteAndShipmentDTO dto = new MatchDetailWithRouteAndShipmentDTO();
                    dto.setMatchDetail(matchDetail);
                    
                    // 设置路线信息
                    if (matchDetail.getRouteId() != null && matchDetail.getRouteId() != -1) {
                        dto.setRoute(routeMap.get(matchDetail.getRouteId()));
                    }
                    
                    // 设置货物信息
                    if (matchDetail.getShipmentId() != null) {
                        dto.setShipment(shipmentMap.get(matchDetail.getShipmentId()));
                    }
                    
                    return dto;
                })
                .collect(Collectors.toList());
    }
}